package com.community.property.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Properties;

@Component
@Slf4j
public class RabbitMQDebugger {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private AmqpAdmin amqpAdmin;

    /**
     * 检查队列状态
     */
    public void checkQueueStatus() {
        try {
            Properties repairQueueProps = amqpAdmin.getQueueProperties("repair.queue");
            Properties dlxQueueProps = amqpAdmin.getQueueProperties("repair.dlx.queue");
            
            log.info("📊 队列状态检查:");
            log.info("repair.queue 消息数量: {}", repairQueueProps != null ? repairQueueProps.get("QUEUE_MESSAGE_COUNT") : "队列不存在");
            log.info("repair.dlx.queue 消息数量: {}", dlxQueueProps != null ? dlxQueueProps.get("QUEUE_MESSAGE_COUNT") : "队列不存在");
            
        } catch (Exception e) {
            log.error("检查队列状态失败: {}", e.getMessage());
        }
    }

    /**
     * 手动触发死信队列处理（测试用）
     */
    public void triggerDlxProcessing() {
        try {
            // 获取死信队列中的消息数量
            Properties props = amqpAdmin.getQueueProperties("repair.dlx.queue");
            if (props != null) {
                int messageCount = (int) props.get("QUEUE_MESSAGE_COUNT");
                log.info("死信队列当前有 {} 条消息等待处理", messageCount);
            }
        } catch (Exception e) {
            log.error("触发死信队列处理失败: {}", e.getMessage());
        }
    }
}